﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.SearchModels;
using DormMS.Model.ViewModels;
using Infrastructure.Dao;
using SqlSugar;

namespace DormMS.Service
{
    public class BedService : IBedService
    {
        private SqlSugarClient db;

        /// <summary>
        /// 同过床位Id修改床位状态
        /// </summary>
        /// <param name="bed"></param>
        /// <returns></returns>bed
        public int BedEdit(Bed model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update(model).ObjToInt();
                
            }

        }
        /// <summary>
        /// 同过床位Id修改床位状态
        /// </summary>
        /// <param name="bed"></param>
        /// <returns></returns>bed
        public int BedAdd(Bed model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Insert(model).ObjToInt();

            }

        }
        public Bed GetBedById(int Id)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Bed>().SingleOrDefault(x => x.Id == Id);
                return model;
            }
        }

        public List<V_Bed> GetBedList(BedSearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";
                search.OrderByFields = search.OrderByFields;

                var data = db.Queryable<Bed>().JoinTable<Building>((m, s2) => m.BuildingId == s2.Id).JoinTable<Room>((m, s3) => m.RoomId == s3.Id).JoinTable<Room,SysCode>((m,s3,s4) => s3.RoomAttr == s4.Code).Where<Bed,SysCode>((m,s4) => s4.CodeType == "公寓性质").JoinTable<Room,SysCode>((m,s3, s5) => s3.RoomType == s5.Code).Where<Bed, SysCode>((m, s5) => s5.CodeType == "公寓类型").Select<Bed, Building, Room, SysCode, SysCode, V_Bed>((m, s2, s3, s4,s5) => new V_Bed { Id = m.Id, BedNO = m.BedNO, BuildingId = s2.Id, BuildingNO = s2.BuildingNO,RoomId = s3.Id,RoomNO = s3.RoomNO, RoomTypeName = s4.CodeName, RoomAttrName = s5.CodeName, Status = m.Status, });
                
                if (string.IsNullOrEmpty(search.Keywords) == false)
                {
                    data = data.Where<V_Bed>(x => x.BuildingNO == search.Keywords || x.RoomNO == search.Keywords || x.BedNO == search.Keywords);
                }

                var list = data.OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total);
                return list;
            }
        }

        public Bed GetBedListByBedNOAndBuildingIDAndRoomID(string BedNO, int BuildingId, int RoomId)
        {
            using (db = DbConfig.GetInstance())
            {
                var data = db.Queryable<Bed>().SingleOrDefault(x => x.BedNO == BedNO && x.BuildingId == BuildingId && x.RoomId == RoomId);
                return data;
            }
        }

        public List<Bed> GetBedListByRoomID(int RoomID)
        {
            using (db = DbConfig.GetInstance())
            {
                var data = db.Queryable<Bed>().OrderBy("BuildingID,RoomID,Id").ToList();
                return data;
            }
        }
    }
}
